//
//  LXUserInfoEditView.m
//  cat
//
//  Created by imera on 16/3/14.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXUserInfoEditView.h"
#import "UIImageView+WebCache.h"

@interface LXUserInfoEditView()
/** 栏目标题 */
@property (nonatomic, weak) UILabel *titleLabel;
/** 分割线 */
@property (nonatomic, weak) UIView *dividerView;

@property (nonatomic, copy) NSString *title;

/** 栏目内容 */
@property (nonatomic, weak) UILabel *titleContentLabel;
/** 右侧图片 */
@property (nonatomic, weak) UIImageView *photoImageView;

@end

@implementation LXUserInfoEditView

+ (instancetype)initWithTitle:(NSString *)title image:(NSString *)image
{
    return [[self alloc]initWithTitle:title image:image];
}

- (instancetype)initWithTitle:(NSString *)title image:(NSString *)image
{
    self.title = title;
    _image = image;
    return [self init];
}

+ (instancetype)initWithTitle:(NSString *)title titleContent:(NSString *)titleContent
{
    return [[self alloc]initWithTitle:title titleContent:titleContent];
}

- (instancetype)initWithTitle:(NSString *)title titleContent:(NSString *)titleContent
{
    self.title = title;
    _titleContent = titleContent;
    return [self init];
}

- (void)setTitleContent:(NSString *)titleContent
{
    if (titleContent != nil) {
        _titleContent = titleContent;

        [self layoutSubviews];
    }
}

- (void)setImage:(NSString *)image
{
    if (image != nil) {
        _image = image;
        
        [self layoutSubviews];
    }
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor whiteColor];
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews
{
    UILabel *titleLabel = [[UILabel alloc]init];
    [self addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    UILabel *titleContentLabel = [[UILabel alloc]init];
    titleContentLabel.hidden = NO;
    [self addSubview:titleContentLabel];
    self.titleContentLabel = titleContentLabel;
    
    UIImageView *photoImageView = [[UIImageView alloc]init];
    photoImageView.hidden = YES;
    [self addSubview:photoImageView];
    self.photoImageView = photoImageView;
    
    UIView *dividerView = [[UIView alloc]init];
    self.dividerView = dividerView;
    [self addSubview:dividerView];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    /** 标题 */
    UILabel *titleLabel = self.titleLabel;
    titleLabel.text = self.title;
    titleLabel.font = [UIFont systemFontOfSize:15];

    CGSize titleLabelSize = [titleLabel.text sizeWithAttributes:@{NSFontAttributeName:titleLabel.font}];
    CGFloat titleLabelW = titleLabelSize.width;
    CGFloat titleLabelH = titleLabelSize.height;
    CGFloat titleLabelX = 10;
    CGFloat titleLabelY = (self.frame.size.height - titleLabelH) / 2;
    titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
    
    /** 标题内容 */
    UILabel *titleContentLabel = self.titleContentLabel;
    titleContentLabel.font = [UIFont systemFontOfSize:15];
    titleContentLabel.text = self.titleContent;
    titleContentLabel.textColor = [UIColor grayColor];
    
    CGSize titleContentLabelSize = [titleContentLabel.text sizeWithAttributes:@{NSFontAttributeName:titleContentLabel.font}];
    CGFloat titleContentLabelW = titleContentLabelSize.width;
    CGFloat titleContentLabelH = titleContentLabelSize.height;
    CGFloat titleContentLabelX = self.frame.size.width - titleContentLabelW - 10;
    CGFloat titleContentLabelY = (self.frame.size.height - titleContentLabelH) / 2;
    titleContentLabel.frame = CGRectMake(titleContentLabelX, titleContentLabelY, titleContentLabelW, titleContentLabelH);
    

    /** 右侧图片 */
    UIImageView *photoImageView = self.photoImageView;
    if (self.image != nil) {
        [photoImageView sd_setImageWithURL:[NSURL URLWithString:self.image]];
    }
    
    CGFloat photoImageViewW = 40;
    CGFloat photoImageViewH = 40;
    CGFloat photoImageViewX = self.frame.size.width - photoImageViewW - 10;
    CGFloat photoImageViewY = (self.frame.size.height - photoImageViewH) / 2;
    photoImageView.frame = CGRectMake(photoImageViewX, photoImageViewY, photoImageViewW, photoImageViewH);
    
    //图片圆角
    photoImageView.layer.masksToBounds = YES;
    photoImageView.layer.cornerRadius = photoImageViewW * 0.5;
    
    /** 分割线 */
    UIView *dividerView = self.dividerView;
    dividerView.backgroundColor = [UIColor grayColor];
    dividerView.alpha = 0.2;
    
    CGFloat dividerViewW = self.frame.size.width;
    CGFloat dividerViewH = 1;
    CGFloat dividerViewX = 0;
    CGFloat dividerViewY = self.frame.size.height - dividerViewH;
    dividerView.frame = CGRectMake(dividerViewX, dividerViewY, dividerViewW, dividerViewH);
}

- (void)setIsPhoto:(bool)isPhoto
{
    _isPhoto = isPhoto;
    self.photoImageView.hidden = NO;
    self.titleContentLabel.hidden = YES;
}

- (void)setIsDivder:(bool)isDivder
{
    _isDivder = isDivder;
    self.dividerView.hidden = !isDivder;
}
@end
